package com.xiaomage.view.draw7

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.view.View
import android.view.animation.LinearInterpolator

/**
 * Created by Administrator on 2018/1/6.
 */
class PaintView(context: Context): View(context) {
    private var phase=0f
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var paint= getPaint()

        //绘制一条红线
        canvas.drawLine(100f,0f,100f,2500f,paint)

        paint.strokeWidth=80f

        //Cap.SQUARE方形线冒
        paint.strokeCap=Paint.Cap.SQUARE
        canvas.drawLine(100f,200f,500f,200f,paint)

        //Cap.ROUND:圆形线冒
        paint.strokeCap= Paint.Cap.ROUND
        canvas.drawLine(100f,300f,500f,300f,paint)

        //Cap.BUTT:无线冒
        paint.strokeCap= Paint.Cap.BUTT
        canvas.drawLine(100f,100f,500f,100f,paint)

        val path=Path()
        path.moveTo(100f,400f)
        path.lineTo(400f,400f)
        path.lineTo(100f,700f)
        //结合处为锐角，默认为锐角：Join.MITER
        paint.strokeJoin=Paint.Join.MITER
        canvas.drawPath(path,paint)

        val path2=Path()
        path.moveTo(100f,800f)
        path.lineTo(400f,800f)
        path.lineTo(100f,1100f)
        //结合处为圆弧：Join.ROUND
        paint.strokeJoin= Paint.Join.ROUND
        canvas.drawPath(path,paint)

        val path3=Path();
        path3.moveTo(100f,1200f)
        path3.lineTo(400f,1200f)
        path3.lineTo(100f,1500f)
        //结合处为斜角:Join.BEVEL
        paint.strokeJoin=Paint.Join.BEVEL
        canvas.drawPath(path3,paint)

        //圆形拐角效果：Path.Effect.CornerPathEffect
        paint.strokeWidth=8f
        val path4=Path()
        path4.moveTo(100f,1600f)
        path4.lineTo(400f,1600f)
        path4.lineTo(100f,1900f)
        //100度
        paint.pathEffect=CornerPathEffect(100f)
        canvas.drawPath(path4,paint)
        paint.pathEffect=CornerPathEffect(200f)
        canvas.drawPath(path4,paint)

        //虚线效果Path.DashPathEffect
        val path5=Path()
        path5.moveTo(100f,2000f)
        path5.lineTo(600f,2150f)
        path5.lineTo(100f,2300f)
        paint.reset()
        paint= getPaint()
        canvas.drawPath(path5,paint)

        canvas.translate(-100f,0f)
        paint.pathEffect=DashPathEffect(floatArrayOf(20f, 10f, 100f, 100f),0f)
        canvas.drawPath(path5,paint)

        canvas.translate(-100f,0f)
        //开始绘制的偏移值
        paint.pathEffect=DashPathEffect(floatArrayOf(20f, 10f, 100f, 100f),phase)
        canvas.drawPath(path5,paint)

        //印章效果：PathDashPathEffect
        //印章路径
        val path7=Path()
        path7.moveTo(0f,20f)
        path7.lineTo(10f,0f)
        path7.lineTo(20f,20f)
        path7.close()
        path7.addCircle(0f,0f,3f,Path.Direction.CCW)
        //原始路径
        paint.reset()
        paint=getPaint()
        val path8=Path()
        path8.moveTo(300f,2800f)
        path8.lineTo(400f,2600f)
        path8.lineTo(500f,2800f)


        canvas.translate(200f,0f)
        canvas.drawPath(path8,paint)
        canvas.translate(0f,300f)
        paint.pathEffect=PathDashPathEffect(path7,35f,0f,
                PathDashPathEffect.Style.TRANSLATE)
        canvas.drawPath(path8,paint)











    }

    public fun startAnimator(){
        val valueAnimator=ValueAnimator.ofFloat(0f,230f)
        valueAnimator.repeatCount=ValueAnimator.INFINITE
        valueAnimator.duration=2000
        valueAnimator.interpolator=LinearInterpolator()
        valueAnimator.addUpdateListener {
            animator->
            phase=animator.animatedValue.toString().toFloat()
            postInvalidate()
        }
        valueAnimator.start()
    }

    private fun getPaint():Paint{
        val paint= Paint()
        //设置是否抗锯齿
        paint.isAntiAlias=true
        //设置颜色
        paint.color= Color.parseColor("#fc4c4f")
        //设置宽度
        paint.strokeWidth=2f
        //设置样式
        paint.style=Paint.Style.STROKE
        return paint
    }
}